/* content-script */
console.log('content-script 运行了 ...');
window.onload=function (){
    // 监听消息
    chrome.runtime.onMessage.addListener(function(request, sender, sendResponse)
    {
        //向background.js发送消息
        // var msg = {content:'从content-script发送给background.js',};
        // chrome.runtime.sendMessage(msg,function(response) {
        //     console.log('content get response:',response);
        // });
        console.log(request);
        console.log(sender);
        if (request.hasOwnProperty("cmd")){
            switch (request.cmd){
                case "getAllCookie":
                    sendResponse({"data":getAllLocalCookie()});
                    break;
                case "getAllLocalStorageData":
                    sendResponse({"data":getAllLocalStorageData()});
                    break;
                case "getAllStorageData":
                    sendResponse({"cookie":getAllLocalCookie(),"localStorage":getAllLocalStorageData()});
                    break;
                case "getHtml":
                    sendResponse({"data":getHtml()});
                    break;
                case "screenshot":
                    sendResponse("截图开始中");
                    screenshot();
                    alert("截图结束")
                    break;
                case "log":
                    console.log(request.value);
                    sendResponse("已经在控制台打印出");
                    break;
                default:
                    alert("暂不支持当前命令："+request.cmd);
            }
        }else {
            sendResponse('我收到了你的消息！');
        }
    });
    //监听缓存发生变化
    chrome.storage.onChanged.addListener(function(changes, namespace) {
        for (var key in changes) {
            var storageChange = changes[key];
            console.log('Storage key "%s" in namespace "%s" changed. ' +
                'Old value was "%s", new value is "%s".',
                key,
                namespace,
                storageChange.oldValue,
                storageChange.newValue);
        }
    });
    //得到所有cookie
    function getAllLocalCookie(){
        const dataCookie = {};
        const cookieStr = document.cookie;
        if(cookieStr != null && cookieStr !== ""){
            const cookieArr = cookieStr.split('; ');
            for (const cookieArrKey in cookieArr) {
                const everyCookieStrArr = cookieArr[cookieArrKey].split("=");
                dataCookie[everyCookieStrArr[0]] = everyCookieStrArr[1];
            }
        }
        return dataCookie;
    }
//得到所有LocalStorage缓存
    function getAllLocalStorageData(){
        const localStorageData = {};
        for (let i = 0; i < localStorage.length; i++) {
            const key = localStorage.key(i); // 获取本地存储的Key
            // localStorage[key]; // 获取本地存储的value
            localStorageData[key]=localStorage[key];
        }
        return localStorageData;
    }
    //得到当前页面html代码
    function getHtml(){
        return document.documentElement.outerHTML;
    }
    //截屏
    function screenshot(){
        const saveFile = function (data,filename) {
            let save_link=document.createElementNS('http://www.w3.org/1999/xhtml','a');  //img表示生成img元素或a元素或则可以放图片地址的元素
            save_link.href=data;              //img  元素中图片引入用src，a元素中图片的引入用href等等
            save_link.download=filename;    // 下载的名称
            var event = document.createEvent('MouseEvents');//创建event事件
            console.log(event);
            //initMouseEvent 方法用于初始化通过 DocumentEvent 接口创建的 MouseEvent 的值, 详见下文：
            event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            save_link.dispatchEvent(event);
           // console.log(save_link)// &lt;a href=data:image/jpeg:base64.....&gt;&lt;/a&gt;
        }
        html2canvas(document.documentElement,{useCORS:true})
            .then(canvas => {
                dataUrl=canvas.toDataURL("image/jpeg", 1.0);  //图片二进制格式
                saveFile(dataUrl, document.title+"截图"+'.jpg');
            });

    }
}

